#ifndef __WAVDEVICE_H
#define __WAVDEVICE_H

#include <windows.h>
#include <mmsystem.h>

extern unsigned char flip_byte[];
extern unsigned short alaw_to_s16[];

inline unsigned short aLawToS16( unsigned char c ) { return alaw_to_s16[c]; }
inline unsigned char FlipByte( unsigned char c ) { return flip_byte[c]; }

#define NUM_OF_BUFFERS 10

class WaveOutPcm16MonoDevice
{
public:
    WaveOutPcm16MonoDevice( unsigned long aBufSize );
    ~WaveOutPcm16MonoDevice();
    void Write( const unsigned char *buf, unsigned long nBufLen );
    unsigned char *GetBuffer();
    void BufferIsReady( const unsigned char *buf, unsigned long nBufLen );
    operator void *() const { return hwave; }

private:
    struct wvhdrknot { WAVEHDR wvhdr; wvhdrknot *next; };
    wvhdrknot *wvhdrlist;
    void clearWvhdrlist( wvhdrknot *knot );

    unsigned long nBufSize;
    unsigned char *buf[NUM_OF_BUFFERS];
    WAVEHDR wvhdr[NUM_OF_BUFFERS];
    enum { bufstate_free, bufstate_filled, bufstate_played } stateBuf[NUM_OF_BUFFERS];
    HWAVEOUT hwave;
};

#endif // __WAVDEVICE_H
